package com.zxl.always.on.display.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import com.alibaba.sdk.android.feedback.impl.FeedbackAPI;
import com.zxl.always.on.display.R;
import com.zxl.always.on.display.base.BaseAppCompatActivity;
import com.zxl.always.on.display.constant.ICustomAction;
import com.zxl.always.on.display.constant.ISharedPreferences;
import com.zxl.always.on.display.constant.LockerSetting;
import com.zxl.always.on.display.model.ClockStyle;
import com.zxl.always.on.display.model.bean.ClockInfo;
import com.zxl.always.on.display.track.MobclickAgentHelper;
import com.zxl.always.on.display.track.StatisticsConstant;
import com.zxl.always.on.display.track.StatisticsType;
import com.zxl.always.on.display.ui.widget.SettingItem;
import com.zxl.always.on.display.ui.widget.SettingPreviewItemView;

/**
 * Created by Administrator on 2018/2/28.
 */

public class MainActivity extends BaseAppCompatActivity implements View.OnClickListener {

    private SettingItem mSwitchSettingItem, mShowTimerSettingItem, mShowTimerContentSettingItem, mFeedbackItem;

    private SettingPreviewItemView mDigitalClockView, mAnalogClockView, mImageClockView;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (ICustomAction.ACTION_CLOCK_CHANGE.equals(intent.getAction())) {
                ClockStyle clockStyle = (ClockStyle) intent.getSerializableExtra("data");
                setClockStyle(clockStyle.getId(), true);
                MobclickAgentHelper.onEvent(MainActivity.this, StatisticsType.SETTINGS, StatisticsConstant.Event.CURRENT_STYLE, clockStyle.name());
            } else if (ICustomAction.ACTION_TIMER_CHANGE.equals(intent.getAction())) {
                String summary = LockerSetting.sStartHour + ":" + LockerSetting.sStartMin + " - " + LockerSetting.sEndHour + ":" + LockerSetting.sEndMin;
                mShowTimerContentSettingItem.setSummary(summary);
                MobclickAgentHelper.onEvent(MainActivity.this, StatisticsType.SETTINGS, StatisticsConstant.Event.TIMER_DURATION, summary);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setMainActivity(true);

        mFeedbackItem = (SettingItem) findViewById(R.id.feed_back);
        mFeedbackItem.setOnItemClickListener(this);

        mSwitchSettingItem = (SettingItem) findViewById(R.id.main_switch);
        mSwitchSettingItem.setOnItemClickListener(this);
        mSwitchSettingItem.setChecked(LockerSetting.sIsSwitchStatus);

        int itemSize = getResources().getDisplayMetrics().widthPixels / 2;

        mDigitalClockView = (SettingPreviewItemView) findViewById(R.id.preview1);
        mDigitalClockView.setClockStyle(ClockStyle.CLOCK_DIGITAL, itemSize);
        mDigitalClockView.setOnClickListener(this);

        mAnalogClockView = (SettingPreviewItemView) findViewById(R.id.preview2);
        mAnalogClockView.setClockStyle(ClockStyle.CLOCK_ANALOG, itemSize);
        mAnalogClockView.setOnClickListener(this);

        mImageClockView = (SettingPreviewItemView) findViewById(R.id.preview3);
        mImageClockView.setClockStyle(ClockStyle.CLOCK_IMAGE, itemSize);
        mImageClockView.setOnClickListener(this);

        setClockStyle(ClockInfo.getClockStyle().getId(), false);

        mShowTimerSettingItem = (SettingItem) findViewById(R.id.show_timer_switch);
        mShowTimerSettingItem.setChecked(LockerSetting.sShowTimerSetting);
        mShowTimerSettingItem.setOnItemClickListener(this);

        mShowTimerContentSettingItem = (SettingItem) findViewById(R.id.show_timer_content);
        mShowTimerContentSettingItem.setOnItemClickListener(this);
        mShowTimerContentSettingItem.setVisibility(!LockerSetting.sShowTimerSetting ? View.VISIBLE : View.GONE);
        findViewById(R.id.show_timer_switch_underline).setVisibility(!LockerSetting.sShowTimerSetting ? View.VISIBLE : View.GONE);
        mShowTimerContentSettingItem.setSummary(LockerSetting.sStartHour + ":" + LockerSetting.sStartMin + " - " + LockerSetting.sEndHour + ":" + LockerSetting.sEndMin);

        IntentFilter intentFilter = new IntentFilter(ICustomAction.ACTION_CLOCK_CHANGE);
        intentFilter.addAction(ICustomAction.ACTION_TIMER_CHANGE);
        registerReceiver(mReceiver, intentFilter);
    }

    @Override
    public void onClick(View v) {
        if (v == mImageClockView) {
            ImageSettingActivity.startPage(this);
        } else if (SettingPreviewItemView.class.isInstance(v)) {
            ClockSettingsActivity.startPage(this, (SettingPreviewItemView) v);
        } else if (v == mSwitchSettingItem) {
            mSwitchSettingItem.toggle();
            LockerSetting.saveLockerSettings(ISharedPreferences.SETTINGS_SWITCH_STATUS, mSwitchSettingItem.isChecked());
            MobclickAgentHelper.onEvent(this, StatisticsType.SETTINGS, StatisticsConstant.Event.SWITCH_STATUS, mSwitchSettingItem.isChecked() + "");
        } else if (v == mShowTimerSettingItem) {
            boolean isChecked = !mShowTimerSettingItem.isChecked();
            mShowTimerSettingItem.setChecked(isChecked);
            mShowTimerContentSettingItem.setVisibility(!isChecked ? View.VISIBLE : View.GONE);
            findViewById(R.id.show_timer_switch_underline).setVisibility(!isChecked ? View.VISIBLE : View.GONE);
            LockerSetting.saveLockerSettings(ISharedPreferences.SETTINGS_SHOW_TIMER_SETTING, isChecked);
            MobclickAgentHelper.onEvent(this, StatisticsType.SETTINGS, StatisticsConstant.Event.TIMER_SWITCH_STATUS, isChecked + "");
        } else if (v == mShowTimerContentSettingItem) {
            TimerSettingsActivity.startPage(this);
        } else if (v == mFeedbackItem) {
            FeedbackAPI.openFeedbackActivity();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }

    public void setClockStyle(int position, boolean isRefreshStyle) {
        if (position == ClockStyle.CLOCK_DIGITAL.getId()) {
            mDigitalClockView.setChecked(true);
            if (isRefreshStyle) {
                mDigitalClockView.refreshClockStyle();
            }
            mAnalogClockView.setChecked(false);
            mImageClockView.setChecked(false);
        } else if (position == ClockStyle.CLOCK_ANALOG.getId()) {
            mAnalogClockView.setChecked(true);
            if (isRefreshStyle) {
                mAnalogClockView.refreshClockStyle();
            }

            mDigitalClockView.setChecked(false);
            mImageClockView.setChecked(false);
        } else if (position == ClockStyle.CLOCK_IMAGE.getId()) {
            mImageClockView.setChecked(true);
            if (isRefreshStyle) {
                mImageClockView.refreshClockStyle();
            }
            mDigitalClockView.setChecked(false);
            mAnalogClockView.setChecked(false);
        }
    }
}
